import { defineStore } from 'pinia'
import Cache from '@/global/Cache'
import * as CacheKey from '@/global/CacheKey'

export default defineStore('dict', {
  state: () => {
    return {
      roles: Cache.get(CacheKey.DICT_ROLES, [])
    };
  },
  actions: {
    loadRoles() {
      const currentRoles = Cache.get(CacheKey.DICT_ROLES, JSON.stringify([]))
      this.$patch({ roles: JSON.parse(currentRoles) });
    },
    /**
     * 保存角色信息到缓存并更新组件状态
     *
     * @param newState 新的角色数组
     */
    saveRoles(newState: any[]) {
      this.roles = newState;
      Cache.save(CacheKey.DICT_ROLES, this.roles);
      // this.$patch({ roles: newState });
    },
    getRoles(): any[] {
      return Cache.get(CacheKey.DICT_ROLES, []);
    },
    resetState() {
      this.$reset();
    },
  }
})